from app.database.database import SessionLocal, engine
from app.models import models

# 确保表存在
models.Base.metadata.create_all(bind=engine)

SAMPLE_COUNT = 15

def ensure_records(session, model, factory):
    current = session.query(model).count()
    needed = max(0, SAMPLE_COUNT - current)
    if needed <= 0:
        print(f"{model.__tablename__}: 已有 {current} 条，跳过")
        return
    items = [factory(i + current + 1) for i in range(needed)]
    session.add_all(items)
    session.commit()
    print(f"{model.__tablename__}: 新增 {needed} 条，合计 {current + needed} 条")

def main():
    session = SessionLocal()
    try:
        ensure_records(session, models.Winery, lambda idx: models.Winery(
            name=f"示例酒庄 {idx}", location=f"产区 {idx}", contact=f"1380000{idx:04d}", description=f"这是示例酒庄 {idx} 的介绍"
        ))

        ensure_records(session, models.Vineyard, lambda idx: models.Vineyard(
            name=f"地块 {idx}", area=10.0 + idx, grape_variety=["赤霞珠","梅洛","黑皮诺","霞多丽"][idx % 4],
            planting_year=str(2010 + (idx % 10)), soil_type=["砂壤土","黏壤土","砾石土"][idx % 3], location=f"坐标 {idx}"
        ))

        ensure_records(session, models.Harvest, lambda idx: models.Harvest(
            vineyard_name=f"地块 {((idx-1)%SAMPLE_COUNT)+1}", harvest_date=f"2024-09-{(idx%28)+1:02d}", quantity=500 + idx * 10,
            quality_grade=["特级","一级","二级","三级"][idx % 4], harvest_method=["手工采收","机械采收"][idx % 2],
            weather=["晴","多云","小雨"][idx % 3], notes=f"采收批次备注 {idx}"
        ))

        ensure_records(session, models.Preprocessing, lambda idx: models.Preprocessing(
            batch_number=f"PP-{idx:04d}", process_date=f"2024-10-{(idx%28)+1:02d}", process_type=["分选","清洗","破碎","压榨"][idx % 4],
            temperature=12.5 + (idx % 5), duration=1.5 + (idx % 3), operator=f"操作员{idx}", notes=f"前处理备注 {idx}"
        ))

        ensure_records(session, models.Fermentation, lambda idx: models.Fermentation(
            batch_number=f"F-{idx:04d}", start_date=f"2024-11-{(idx%28)+1:02d}", end_date=f"2024-12-{(idx%28)+1:02d}",
            temperature=18 + (idx % 6), yeast_type=["酵母A","酵母B","酵母C"][idx % 3], alcohol_content=11.0 + (idx % 5) * 0.5,
            status=["进行中","已完成","异常"][idx % 3]
        ))

        ensure_records(session, models.Storage, lambda idx: models.Storage(
            batch_number=f"S-{idx:04d}", storage_date=f"2025-01-{(idx%28)+1:02d}", storage_type=["橡木桶","不锈钢罐","玻璃瓶"][idx % 3],
            temperature=14 + (idx % 5), humidity=65 + (idx % 10), duration=f"{3 + (idx % 12)}个月", notes=f"贮存备注 {idx}"
        ))

        ensure_records(session, models.Bottling, lambda idx: models.Bottling(
            batch_number=f"B-{idx:04d}", bottling_date=f"2025-02-{(idx%28)+1:02d}", bottle_type=["750ml","375ml","1.5L"][idx % 3],
            quantity=1000 + idx * 10, label_info=f"标签批次 {idx}", packaging_type=["单瓶","礼盒","木箱"][idx % 3], notes=f"罐装备注 {idx}"
        ))

        ensure_records(session, models.QualityCheck, lambda idx: models.QualityCheck(
            batch_number=f"QC-{idx:04d}", check_date=f"2025-03-{(idx%28)+1:02d}", inspector=f"检验员{idx}",
            alcohol_content=12 + (idx % 6) * 0.3, acidity=5.0 + (idx % 5) * 0.1, sugar_content=2.0 + (idx % 5) * 0.2,
            result=["合格","不合格","待复检"][idx % 3], notes=f"质检备注 {idx}"
        ))

        ensure_records(session, models.Order, lambda idx: models.Order(
            order_number=f"ORD-{idx:05d}", customer_name=f"客户{idx}", product_name=f"产品{(idx%5)+1}", quantity=10 + (idx % 20),
            order_date=f"2025-04-{(idx%28)+1:02d}", delivery_date=f"2025-05-{(idx%28)+1:02d}", status=["待确认","已确认","生产中","已完成","已交付"][idx % 5],
            notes=f"订单备注 {idx}"
        ))

        ensure_records(session, models.Logistics, lambda idx: models.Logistics(
            tracking_number=f"LGS-{idx:06d}", order_number=f"ORD-{idx:05d}", carrier=["顺丰","中通","圆通","申通","德邦"][idx % 5],
            shipping_date=f"2025-06-{(idx%28)+1:02d}", delivery_date=f"2025-06-{(idx%28)+2:02d}", status=["已发货","运输中","已签收","异常"][idx % 4],
            notes=f"物流备注 {idx}"
        ))

    finally:
        session.close()

if __name__ == "__main__":
    main()
